<?php
require_once Config::ROOT . '/modules/AdvModule.php';

class TinyMCE extends AdvModule {
    const CONFIGURATIONS = 'tinyMCEs';
    
    protected $tinyMCEs = array('id' => 'iPN+', 'name' => 'sN',
            'version' => 'sN', 'configuration' => 'TN', 'example' => 'T');
    protected $tinyMCEPresentation = array(
        'example' => array(
            'hiddenInList',
            'textArea',
            'WYSIWYG'),
        'configuration' => array(
            'hiddenInList',
            'textArea',
            'noWYSIWYG')
    );
    protected $tinyMCERecord;
    
    public function initWysiwyg($id = null) {
        $wysiwyg = $this->getWysiwyg($id);
        // TODO: include chosen version of tinyMCE
        $this->js('/utils/tiny_mce/tiny_mce.js', false);
        $this->js('/ajax/admin/content/tiny-mce/wysiwyg/' . (int) $id . '/',
                false);
    }
    
    public function displayBlank($param = null) {
        $url = Modules::getModule('View')->getUrl();
        if (isset($url[1]) && ($url[1] == 'wysiwyg')) {
            $this->displayAdminWysiwyg(isset($url[2]) ? $url[2] : null);
        }
        parent::displayBlank($param);
    }
    
    public function displayAdminWysiwyg($id = null) {
        header('Content-Type: text/javascript');
        $wysiwyg = $this->getWysiwyg($id);
        echo $wysiwyg['configuration'];
    }
    
    private function getWysiwyg($id) {
        if (!$id || is_numeric($id)) {
            $id = 1;
        }
        $cols = array();
        $data = &$this->dbdriver->select(self::CONFIGURATIONS, $cols,
                '`id` = ' . (int) $id);
        return isset($data[0]) ? $data[0] : null;
    }
}